var a = getApp(), t = require("../../../mybehaviors/location/location");

Page({
    behaviors: [ t ],
    data: {
        goods: [],
        purchaseTime: 108e6,
        timeData: {},
        page: 1,
        isLoadMore: !0,
        totalPage: 0,
        city: ""
    },
    onLoad: function(a) {
        a.city && this.setData({
            city: a.city
        }), this.recommendList();
    },
    onTimeChange: function(a) {
        this.setData({
            timeData: a.detail
        });
    },
    recommendList: function() {
        var t = this, o = {
            page: this.data.page,
            city: this.data.city
        }, e = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByTimeEndAscGoods");
        a._postNetWork({
            url: e,
            data: o,
            success: function(a) {
                var o = a.data;
                if (wx.hideLoading(), wx.hideNavigationBarLoading(), wx.stopPullDownRefresh(), 1 == o.result) {
                    var e;
                    t.data.totalPage = o.data.last_page, t.data.totalPage || (t.data.totalPage = 0);
                    var i = o.data.data, s = new Date(null === (e = i[0]) || void 0 === e ? void 0 : e.promotion_end_at).getTime(), n = new Date().getTime();
                    t.setData({
                        goods: o.data.data,
                        purchaseTime: s - n
                    });
                } else t.data.page = t.data.page - 1, t.isLoadMore = !1, wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1500
                });
            },
            fail: function(a) {
                console.log(a);
            }
        });
    },
    getMoreData: function() {
        var t = this;
        if (this.data.isLoadMore = !1, !(this.data.page >= this.data.totalPage)) {
            this.data.page = this.data.page + 1;
            var o = {
                page: this.data.page,
                city: this.data.city
            }, e = a.getNetAddresss("plugin.yz-supply-cake.frontend.goods.goods.getGoodsByTimeEndAscGoods");
            a._getNetWork({
                url: e,
                data: o,
                success: function(a) {
                    var o = a.data, e = o.data.data;
                    1 == o.result ? (t.data.isLoadMore = !0, t.setData({
                        goods: t.data.goods.concat(e)
                    })) : (t.data.page = t.data.page - 1, t.data.isLoadMore = !1, wx.showToast({
                        icon: "none",
                        title: o.msg,
                        duration: 1500
                    }));
                },
                fail: function(a) {
                    console.log(a);
                }
            });
        }
    },
    onReady: function() {
        var a = this;
        try {
            this._getLocation(function(t, o) {
                var e = t.address_component.city;
                a.data.city || a.setData({
                    city: e
                }), a.recommendList();
            });
        } catch (a) {
            console.log(a), this.recommendList();
        }
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        this.setData({
            isLoadMore: !0,
            page: 1,
            totalPage: 0
        }), this.getMoreData();
    },
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多了");
    },
    onShareAppMessage: function() {}
});